
export default class Variables {

  storedVars = new Map() // 存储的变量

  getPropertyValue(obj1: any, dataToRetrieve: string) {
    return dataToRetrieve.split('.').reduce(function (o, k) {
      const arr = /([^.\n]+)\[(\d*)\]/.exec(k)
      if (arr) {
        return o && o[arr[1]][arr[2]]
      }
      return o && o[k]
    }, obj1)
  }

  // 获取变量
  get(key: string) {
    if (this.storedVars.get(key) == undefined) {
      if (/(\.)/.exec(key)) {
        //a.b , a[0].b, a.b[3].c
        const propertyAccess = /([^.\n]+)\.(.*)/.exec(key) || []
        if (this.storedVars.has(propertyAccess[1])) {
          //a.b, a.b[0].c
          const r3 = this.getPropertyValue(
            this.storedVars.get(propertyAccess[1]),
            propertyAccess[2]
          )
          return r3
        } else if (/([^.\n]+)\[(\d*)\]/.exec(propertyAccess[1])) {
          //a[0].b
          const arrayAccess = /([^.\n]+)\[(\d*)\]/.exec(propertyAccess[1]) || []
          if (this.storedVars.has(arrayAccess[1])) {
            const r3 = this.getPropertyValue(
              this.storedVars.get(arrayAccess[1])[arrayAccess[2]],
              propertyAccess[2]
            )
            return r3
          }
        }
      } else if (/([^.\n]+)\[(\d*)\]/.exec(key)) {
        //a[0]
        const arrayAccess = /([^.\n]+)\[(\d*)\]/.exec(key) || []
        if (this.storedVars.has(arrayAccess[1])) {
          const r3 = this.storedVars.get(arrayAccess[1])
          return r3[arrayAccess[2]]
        }
      }
      return this.storedVars.get(key)
    }
    return this.storedVars.get(key)
  }

  set(key: string, value: any) {
    this.storedVars.set(key, value)
  }

  has(key: string) {
    if (this.storedVars.has(key) == false) {
      if (/(\.)/.exec(key)) {
        //a.b or a[0].b or a.b[0].c
        const propertyAccess = /([^.\n]+)\.(.*)/.exec(key) || []
        if (
          this.storedVars.has(propertyAccess[1]) == false &&
          /([^.\n]+)\[(\d*)\]/.exec(propertyAccess[1])
        ) {
          //arr[0].a
          const arrayAccess = /([^.\n]+)\[(\d*)\]/.exec(propertyAccess[1]) || []
          return this.storedVars.has(arrayAccess[1])
        } else return this.storedVars.has(propertyAccess[1])
      } else if (/([^.\n]+)\[(\d*)\]/.exec(key)) {
        // a[0]
        const arrayAccess = /([^.\n]+)\[(\d*)\]/.exec(key) || []
        return this.storedVars.has(arrayAccess[1])
      }
    }
    return this.storedVars.has(key)
  }

  delete(key: string) {
    if (this.storedVars.has(key)) this.storedVars.delete(key)
    else if (/(\.)/.exec(key)) {
      const propertyAccess = /([^.]+)\.(.*)/.exec(key) || []
      this.storedVars.delete(propertyAccess[1])
    }
  }

  clear() {
    this.storedVars.clear()
  }
}